package com.ruoyi.web.controller.cert;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cert.domain.Cert;
import com.ruoyi.cert.service.ICertService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 证书Controller
 *
 * @author mxh
 * @date 2023-03-26
 */
@Api("证书")
@RestController
@RequestMapping("/cert/cert")
public class CertController extends BaseController {
    @Autowired
    private ICertService certService;

    /**
     * 小程序查询当前登录用户证书
     */
    @GetMapping("/getUserData")
    @ApiOperation("查询当前登录用户证书")
    public AjaxResult getUserData() {
        List<Cert> certList = certService.selectAppletCert(Cert.builder().studentId(getUserId()).status(1).build());
        if (ObjectUtil.isEmpty(certList)){
            return AjaxResult.error("未查询到有效证件","");
        }
        return AjaxResult.success(certList.get(0));
    }

//
//    /**
//     * 查询当前登录用户证书
//     */
//    @GetMapping("/getUserData")
//    @ApiOperation("查询当前登录用户证书")
//    public AjaxResult getUserData() {
//        List<Cert> certList = certService.selectCertList(Cert.builder().studentId(getUserId()).status(1).build());
//        if (ObjectUtil.isEmpty(certList)){
//            return AjaxResult.error("未查询到有效证件","");
//        }
//        return AjaxResult.success(certList.get(0));
//    }

    /**
     * 查询证书列表
     */

    @PostMapping("/list")
    @ApiOperation("查询证书列表")
    public TableDataInfo list(@RequestBody Cert cert) {
        cert.setDeptId(getDeptId());
        List<Cert> list = certService.selectCertList(cert);
        return getDataTable(list);
    }

    /**
     * 导出证书列表
     */
    @PreAuthorize("@ss.hasPermi('cert:cert:export')")
    @Log(title = "证书", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cert cert) {
        cert.setPageSize(1000);
        cert.setDeptId(getDeptId());
        List<Cert> list = certService.selectCertListExport(cert);
        ExcelUtil<Cert> util = new ExcelUtil<Cert>(Cert. class);
        util.exportExcel(response, list, "证书数据");
    }

    /**
     * 获取证书详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取证书详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(certService.selectCertById(id));
    }

    /**
     * 新增证书
     */
    @PreAuthorize("@ss.hasPermi('cert:cert:add')")
    @Log(title = "证书", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增证书")
    public AjaxResult add(@RequestBody Cert cert) {
        cert.setDeptId(getDeptId());
        cert.setApprovalDate(new Date());
        cert.setApprovalBy(getUser().getNickName());
        cert.setApprovalInfo("发证");
        return toAjax(certService.insertCert(cert));
    }

    /**
     * 修改证书
     */
    @PreAuthorize("@ss.hasPermi('cert:cert:edit')")
    @Log(title = "证书", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改证书")
    public AjaxResult edit(@RequestBody Cert cert) {
        cert.setDeptId(getDeptId());
        return toAjax(certService.updateCert(cert));
    }

    /**
     * 删除证书
     */
    @PreAuthorize("@ss.hasPermi('cert:cert:remove')")
    @Log(title = "证书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除证书")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(certService.deleteCertByIds(ids));
    }
}
